﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Hawaii.Ocr.Client;
using OcrSampleApp.Utils;
using OcrSampleApp.ViewModels;

namespace OcrSampleApp.Data
{
    public class MyWord
    {
        public MyWord(OcrWord word)
        {
            MyText = word.Text;

            if (isNumber())
            {
                MyType = ContactType.PhoneNumber;
            }
            else if (isEmail())
            {
                MyType = ContactType.EmailAddress;
            }
            else if (isUrl())
            {
                MyType = ContactType.Url;
            }
            else
            {
                MyType = ContactType.NotContact;
            }
        }

        public string MyText { get; set; }

        public string MyAbsoluteLink { get; set; }

        public ContactType MyType { get; set; }

        public string ButtonText { get; set; }

        public bool isContact()
        {
            return isNumber() || isEmail() || isUrl();
        }

        // checks if MyText is a valid phone number
        public bool isNumber()
        {
            if (MyText.Length < 3)
            {
                return false;
            }

            // first letter has to be numeric or '+'
            Char first = MyText[0];
            if (!Char.IsNumber(first) && !Char.Equals(first, '+'))
            {
                return false;
            }

            String rest = MyText.Substring(1);
            int Num;
            bool isNum = int.TryParse(rest, out Num);
            return isNum;
        }

        // checks if MyText is a valid email address
        public bool isEmail()
        {
            return MyText.Contains("@") && MyText.Contains(".");
        }

        // A naive function for URL validation
        public bool isUrl()
        {
         
            if (MyText.Contains("http://") && Uri.IsWellFormedUriString(MyText, UriKind.Absolute))
            {
                MyAbsoluteLink = MyText;
                return true;
            }

            else if (Uri.IsWellFormedUriString(("http://" + MyText), UriKind.Absolute) && MyText.Contains("."))
            {
                MyAbsoluteLink = "http://" + MyText;
                return true;
            }

            return false;
        }
    }
}
